
package Interfaz;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Queue;

import javax.swing.JLabel;

import Juego.Civil;
import Juego.Juego;
import Juego.Posicion;
import Objetos.Concreto;
import Terrenos.Arena;
import Terrenos.Color;

public class Metodos {
	
	private static Metodos single=new Metodos();
	
	private Graphics2D grafico;
	private int weight;
	private int height;
	private Juego juego;
	private JLabel combustible;
	
	private Metodos(){
		
	}
	
	public static synchronized Metodos get() {
		return single;
	}	
	
	public void cargarNivel(){

				
			juego=new Juego("/Mapas/mapa.txt");
		
		SpriteStore.get().putSprite("Imagenes/sand.png",Arena.class);
		SpriteStore.get().putSprite("Imagenes/mega up.gif",Civil.class);	
		SpriteStore.get().putSprite("Imagenes/black.png",Color.class);
		SpriteStore.get().putSprite("Imagenes/stone.png",Concreto.class);
		combustible.setText(""+juego.Combustible());
		paint();		
	}
	
	public void avanzar(){
		Posicion p1=juego.getPosicionRobot();		
		if(juego.avanzar()){
			try{
				switch(p1.getMira()){
			
				case 'n' :{				
					for(int i=height; i>0; i--){
							paintSinRobot();
							SpriteStore.get().getSprite(juego.getRobot()).draw(grafico, p1.getX()*weight, p1.getY()*height+i);
							Thread.sleep(30);
						}
					break;}
		
				case 'o' :{ 
						for(int i=weight; i>0; i--){
							paintSinRobot();
							SpriteStore.get().getSprite(juego.getRobot()).draw(grafico, p1.getX()*weight+i, p1.getY()*height);
							Thread.sleep(30);
						}
						break;}
		
				case 's' :{ 
						for(int i=height; i>0; i--){
							paintSinRobot();
							SpriteStore.get().getSprite(juego.getRobot()).draw(grafico, p1.getX()*weight, p1.getY()*height-i);
							Thread.sleep(30);
						}
						break;}
				case 'e' :{ 
						for(int i=weight; i>0; i--){
							paintSinRobot();
							SpriteStore.get().getSprite(juego.getRobot()).draw(grafico, p1.getX()*weight-i, p1.getY()*height);
							Thread.sleep(30);
						}
						break;}
				}
			}
			catch (InterruptedException e) {				                  
				 e.printStackTrace();
			}
			combustible.setText(""+juego.Combustible());
			paint();
		}		
	}
	
	public void girarIzquierda(){
		switch(juego.getPosicionRobot().getMira()){
			case 'n': SpriteStore.get().putSprite("Imagenes/mega left.gif",Civil.class);
				break;
			case 'o': SpriteStore.get().putSprite("Imagenes/mega down.gif",Civil.class);
			break;
			case 's': SpriteStore.get().putSprite("Imagenes/mega right.gif",Civil.class);
			break;
			case 'e': SpriteStore.get().putSprite("Imagenes/mega up.gif",Civil.class);
			break;
		}
		juego.girarIzquierda();
		
		paint();
	}
	public void girarDerecha(){
		switch(juego.getPosicionRobot().getMira()){
			case 'n': SpriteStore.get().putSprite("Imagenes/mega right.gif",Civil.class);
			break;
			case 'o': SpriteStore.get().putSprite("Imagenes/mega up.gif",Civil.class);
			break;
			case 's': SpriteStore.get().putSprite("Imagenes/mega left.gif",Civil.class);
			break;
			case 'e': SpriteStore.get().putSprite("Imagenes/mega down.gif",Civil.class);
			break;
			}
		juego.girarDerecha();
		
		paint();
	}
	
	public void setGraphics(Graphics g){
		grafico=(Graphics2D)g;
	}
	
	public void paint(){
		int x=0;
		int y=0;
		for(int i =0;i<10;i++){
			x=weight*i;
			for(int j=0;j<10;j++){
				y=height*j;
				Queue<Object> elem=juego.elements(i, j);
				while(!elem.isEmpty())
				SpriteStore.get().getSprite(elem.poll()).draw(grafico, x, y);
			}
		}
		SpriteStore.get().getSprite(juego.getRobot()).draw(grafico, juego.getPosicionRobot().getX()*weight, juego.getPosicionRobot().getY()*height);
	}
	public void paintSinRobot(){
		int x=0;
		int y=0;
		for(int i =0;i<10;i++){
			x=weight*i;
			for(int j=0;j<10;j++){
				y=height*j;
				Queue<Object> elem=juego.elements(i, j);
				while(!elem.isEmpty())
				SpriteStore.get().getSprite(elem.poll()).draw(grafico, x, y);
			}
		}
	}
	
	public void setDimension(int w,int h){
		weight=w;
		height=h;
	}
	
	public void setLabels(JLabel c){
		combustible=c;
	}
	public boolean Terminamos(){
		return juego.terminamos();
	}
	public boolean ganamos(){
		return juego.ganamos();
	}
}
